package com.shxzz.easy12306.framework.starter.convention.exceptions;

import com.shxzz.easy12306.framework.starter.convention.errorcode.ErrorCode;
import lombok.Getter;
import org.springframework.util.StringUtils;

import java.io.Serial;
import java.util.Optional;

/**
 * 项目异常抽象类
 * @author shxzz
 * @create 2024-01-25-22:31
 */
@Getter
public class AbstractException extends RuntimeException {

    @Serial
    private static final long serialVersionUID = -6889729279088083308L;

    /**
     * 异常码
     */
    public final String errorCode;

    /**
     * 异常信息
     */
    public final String errorMsg;

    AbstractException(String errorMsg, Throwable throwable, ErrorCode errorCode) {
        // 传递异常堆栈信息
        super(errorMsg, throwable);
        this.errorCode = errorCode.code();
        this.errorMsg = Optional.ofNullable(StringUtils.hasLength(errorMsg) ? errorMsg : null).orElse(errorCode.message());
    }
}
